{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 声明元数据\n",
    "与使用 Query 为查询参数声明更多的校验和元数据的方式相同，你也可以使用 Path 为路径参数声明相同类型的校验和元数据。"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Started server process [13988]\n",
      "INFO:     Waiting for application startup.\n",
      "INFO:     Application startup complete.\n",
      "INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:     127.0.0.1:58372 - \"GET /123456?q=hello HTTP/1.1\" 404 Not Found\n",
      "INFO:     127.0.0.1:58384 - \"GET /items/123456?q=hello HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:58385 - \"GET /items/123456?item-query=hello HTTP/1.1\" 200 OK\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Shutting down\n",
      "INFO:     Waiting for application shutdown.\n",
      "INFO:     Application shutdown complete.\n",
      "INFO:     Finished server process [13988]\n"
     ]
    }
   ],
   "source": [
    "from fastapi import FastAPI, Query, Path\n",
    "from typing import Annotated\n",
    "import uvicorn\n",
    "\n",
    "app = FastAPI()\n",
    "\n",
    "\n",
    "@app.get(\"/items/{item_id}\")\n",
    "async def read_item(\n",
    "        item_id: Annotated[int, Path(title=\"The ID of the item to get\")],\n",
    "        q: Annotated[str | None, Query(alias='item-query')] = None\n",
    "):\n",
    "    results = {\"item_id\": item_id}\n",
    "    if q:\n",
    "        results.update({\"q\": q})\n",
    "    return results\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    config = uvicorn.Config(app=app, host='127.0.0.1', port=8000)\n",
    "    server = uvicorn.Server(config)\n",
    "    await server.serve()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}